package com.auto.finance.auth.collect.support;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.auto.finance.auth.collect.CollectProperties;
import com.auto.finance.auth.collect.beans.ApiEntity;
import lombok.Setter;
import lombok.experimental.Accessors;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.web.client.RestTemplate;

@Accessors(chain = true)
public class FeignRestTemplateReporter implements Reporter {

    @Qualifier("reportRestTemplate")
    @Autowired
    RestTemplate restTemplate;

    @Setter
    private CollectProperties properties;

    @Override
    public Result reportApiCollect(ApiEntity entity) {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.valueOf(MediaType.APPLICATION_JSON_UTF8_VALUE));
        HttpEntity<String> requestBody = new HttpEntity<>(JSON.toJSONString(entity), headers);

        Result userInfoResult = JSON.parseObject(restTemplate.postForObject(properties.getReportLocation(), requestBody, String.class),
                new TypeReference<Result>() {
                });

        return userInfoResult;
    }
}
